package com.dayu.gulimall.member.service.impl;

import com.dayu.gulimall.member.dao.MemberLevelDao;
import com.dayu.gulimall.member.entity.MemberLevelEntity;
import com.dayu.gulimall.member.exception.PhoneExistException;
import com.dayu.gulimall.member.exception.UserNameExistException;
import com.dayu.gulimall.member.vo.MemberLoginVo;
import com.dayu.gulimall.member.vo.RegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayu.common.utils.PageUtils;
import com.dayu.common.utils.Query;

import com.dayu.gulimall.member.dao.MemberDao;
import com.dayu.gulimall.member.entity.MemberEntity;
import com.dayu.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    MemberLevelDao memberLevelDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 会员注册方法
     *
     * @param vo
     */
    @Override
    public void regist(RegistVo vo) {
        MemberDao baseMapper = this.baseMapper;
        MemberEntity memberEntity = new MemberEntity();

        MemberLevelEntity memberLevelEntity = memberLevelDao.getDefaultLevel();
        memberEntity.setLevelId(memberLevelEntity.getId());

        //验证唯一性,用户名和手机号
        //为了让controller能感知异常，使用异常机制
        checkPhoneUnique(vo.getPhone());
        checkUsernameUnique(vo.getUserName());

        memberEntity.setUsername(vo.getUserName());
        memberEntity.setMobile(vo.getPhone());

        //密码要进行加密存储,用org.springframework.security.crypto.bcrypt;自带的加密包
        BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(vo.getPassword());
        memberEntity.setPassword(encode);

        baseMapper.insert(memberEntity);
    }

    @Override
    public void checkPhoneUnique(String phone)throws PhoneExistException {
        Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if (count>0){
            throw new UserNameExistException();
        }
    }

    @Override
    public void checkUsernameUnique(String username)throws UserNameExistException {
        Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", username));
        if (count>0){
            throw new UserNameExistException();
        }
    }

    @Override
    public MemberEntity login(MemberLoginVo vo) {
        MemberDao baseMapper = this.baseMapper;
        String loginacct = vo.getLoginacct();
        String password = vo.getPassword();
        MemberEntity memberEntity = baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", loginacct).or().eq("mobile", loginacct));
        if (memberEntity ==null){
            return null;
        }else {
            String password1 = memberEntity.getPassword();
            BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
            //password 明文密码，password1 加密后的密码
            boolean matches = encoder.matches(password, password1);
            if (matches){
                return memberEntity;
            }else {
                return null;
            }
        }
    }

}